/*
*
* ModeSelector
*
* This class controls the pull down selector used to select the drawing mode
*
*/
class ModeSelectorUI {
  
  // ---------------------------------
  // attributes
  // ---------------------------------
  ControlP5 controlp5;
  PApplet parent;
  //ListBox modeSelector;
  Textlabel msLabel;
  MultiList modeSelector;
  DModeManager dModeMan;
  int origX, origY, wid, hei;
  
  // ----------------------------------
  // constructor
  // ----------------------------------
  ModeSelectorUI(ControlP5 cp5, PApplet p55, DModeManager dmm, int x, int y, int w, int h) {
    
    // assign attributes
    controlp5 = cp5;
    parent = p55;
    dModeMan = dmm;
    origX = x;
    origY = y;
    wid = w;
    hei = h;
    
    // draw title box and text
    msLabel = controlp5.addTextlabel("msLabel", "DRAWING MODE", origX + 4, origY + 4);
    msLabel.setColorValue(0);
    
    // create mode selection list box using ControlP5 ListBox class
    modeSelector = controlp5.addMultiList("modeSelector", origX, origY + 16, wid, hei);  // create MultiList
    modeSelector.setId(1);                                                          // set ID of ListBox control group for event listening
    
    // create modeSelector multiList
    // iterate through DModeGroups in master DModeGroup, and call build method
    // on each of them. Note: could call buildModeSelector method on the "master"
    // DModeGroup here, but then everything would be collapsed beneath a single
    // top-level ListButton
    for(int i = 0; i < dmm.masterDMG.dmgChildren.size(); i++) {
      
      DModeGroup dmg = dmm.masterDMG.dmgChildren.get(i);
      
      // check if DModeGroup has children; if so, call recursive build method
      // if not, create button
      if(dmg.dmgChildren == null) {
        MultiListButton button = modeSelector.add(dmg.name, dmg.index);
        formatButton(button, dmg.displayName);
      } else {
        buildModeSelector(modeSelector, dmg);
      } // if
    } // for
    
    modeSelector.hide(); // hide ListBox so it isn't drawn twice; show() called during draw loop
    msLabel.hide();
    
  } // constructor method
  
  // ------------------------------------
  // buildModeSelector
  // recursively builds mode selector multi list
  // ------------------------------------
  void buildModeSelector(MultiListButton parent, DModeGroup dmg) {
    
    // add button for item and set button height
    MultiListButton button = parent.add(dmg.name, dmg.index);
    formatButton(button, dmg.displayName);
    
    // check if DModeGroup has children; if so, recursively call method on children
    if(dmg.dmgChildren != null) {
      for(int j = 0; j < dmg.dmgChildren.size(); j++) {
        buildModeSelector(button, dmg.dmgChildren.get(j));
      } // for
    } // if
    
  } // buildModeSelector

  // ------------------------------------
  // buildModeSelector
  // recursively builds mode selector multi list
  // this method overloaded to take MultiList as argument
  // ------------------------------------
  void buildModeSelector(MultiList parent, DModeGroup dmg) {
    
    // add button for item and set button height
    MultiListButton button = parent.add(dmg.name, dmg.index);
    formatButton(button, dmg.displayName);
    
    // check if DModeGroup has children; if so, recursively call method on children
    if(dmg.dmgChildren != null) {
      for(int j = 0; j < dmg.dmgChildren.size(); j++) {
        buildModeSelector(button, dmg.dmgChildren.get(j));
      } // for
    } // if
    
  } // buildModeSelector
  
  // ------------------------------------
  // formatButton
  // formats MultiListButton
  // ------------------------------------
  void formatButton(MultiListButton button, String label) {
    
    button.setHeight(15);
    button.setColorBackground(color(50, 50));                                 // controls base colour
    button.setColorForeground(color(0, 200, 200, 30));                        // colour when mouse is overtop of item
    button.setColorActive(color(0, 200, 200, 70));                            // colour when item is clicked
    button.setColorCaptionLabel(color(0));                                    // text colour
    button.setLabel(label);                                                   // display label
    
  } // formatButton
  
  // ------------------------------------
  // draw
  // called during the sketch's draw loop
  // ------------------------------------
  void draw() {
  
    // draw list box
    modeSelector.show();
    
    // draw label and bounding box
    msLabel.show();
    noStroke();
    fill(50, 50);
    rect(origX, origY, wid * 4 / 3, 15);
    
  } // draw method  
  
} // ModeSelector class
